package com.software.sqlite.api.admin.v1.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.software.sqlite.api.admin.v1.service.impl.AmV1SysUserService;
import com.software.sqlite.api.admin.v1.vo.AdminV1Result;
import com.software.sqlite.entity.TbMovieServer;
import com.software.sqlite.mapper.TbMovieServerMapper;
import com.software.sqlite.service.impl.YstMovieService;
import com.software.sqlite.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Api(value = "管理端-影视api", tags = "管理端-影视api")
@RestController
@Slf4j
@RequestMapping("/admin/v1/ystapi")
@CrossOrigin
public class YstApiController extends BaseController {

    @Resource
    private YstMovieService ystMovieService;
    @Resource
    private TbMovieServerMapper movieServerMapper;


    @ApiOperation(value = "apiTest", notes = "apiTest")
    @PostMapping("/apiTest")
    public AdminV1Result apiTest(@RequestBody Map<String,Object> paramsMap)throws Exception{
        if(paramsMap==null || (paramsMap!=null && paramsMap.size()==0)){
            return AdminV1Result.fail("提交参数错误");
        }
        String token=CommonUtils.parseStringData(paramsMap.get("token"));
        Integer apiNum=CommonUtils.parseIntegerData(paramsMap.get("apiNum"));
        String requestData=CommonUtils.parseStringData(paramsMap.get("requestData"));
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        TbMovieServer movieServer = new TbMovieServer();
        List<TbMovieServer> list = movieServerMapper.selectListBySQL(movieServer);
        if(list.size()==0)
        {
            result.setCode(1);
            result.setMsg("船端影视服务URL请先在服务配置保存好后测试！");
            return result;
        }
        String shipServerUrl = list.get(0).getServerUrl();
        if(CommonUtils.isBlank(requestData)){
            result.setCode(1);
            result.setMsg("请求参数不能为空");
            return result;
        }
        HashMap<String,Object> map;
        try{
            map=JSONObject.parseObject(requestData,HashMap.class);
        }catch (Exception e){
            result.setCode(1);
            result.setMsg("请求参数错误【"+requestData+"】");
            return result;
        }
        if(map==null){
            result.setCode(1);
            result.setMsg("请求参数不能为空");
            return result;
        }
        if(apiNum==1){
            if(!map.containsKey("MAC"))
            {
                result.setCode(1);
                result.setMsg("MAC请求参数不能为空");
                return result;
            }
            Map<String, Object> regitsterDeviceParams = new LinkedHashMap<>();
            String uuid=CommonUtils.parseStringData(map.get("MAC"));
            /**
             * {
             * 	"C": "Device",
             * "D": {
             * "IP": "192.168.1.1",
             * "MAC": "DD:DD:DD:DD:DD:DD",
             * "ModelNo": "BOX-002",
             * "Title": "BOX-002",
             * "UniqueCode": "b562e3d8a4314e9e9ae6c3648855b2e2",
             * "Version": 1.23
             * },
             * "F": "Regdevice",
             * "M": "DD:DD:DD:DD:DD:DD",
             * "U": "b562e3d8a4314e9e9ae6c3648855b2e2"
             * }
             */
            String uuidMac="";
            if(uuid.length()<12)
            {
                uuidMac= UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
            }else{
                uuidMac=uuid.toUpperCase();
            }
            String mac=uuidMac.substring(0,2)+":"+uuidMac.substring(2,4)+":"+uuidMac.substring(4,6)+":"+uuidMac.substring(6,8)+":"+uuidMac.substring(8,10)+":"+uuidMac.substring(10,12);
            regitsterDeviceParams.put("C", "Device");
            Map<String, Object> D = new LinkedHashMap<>();
            D.put("IP", "192.168.1.1");
            D.put("MAC", mac);
            D.put("ModelNo", "BOX-001");
            D.put("Title", "BOX-001");
            D.put("UniqueCode", uuid);
            D.put("Version", 1.23);
            regitsterDeviceParams.put("D", D);
            regitsterDeviceParams.put("F", "Regdevice");
            regitsterDeviceParams.put("M", mac);
            regitsterDeviceParams.put("U", uuid);
            String resultString = ystMovieService.getRegitsterDeviceShipServer(shipServerUrl, regitsterDeviceParams);
            return AdminV1Result.success(resultString);
        }
        if(apiNum==2){
            if(!map.containsKey("MAC"))
            {
                result.setCode(1);
                result.setMsg("MAC请求参数不能为空");
                return result;
            }
            Map<String, Object> urlParams = new LinkedHashMap<>();
            String uuid=CommonUtils.parseStringData(map.get("MAC"));
            /**
             *{
             * "C": "Device",
             * "D": {
             * "AudioOut": 2.1
             * },
             * "F": "SetDeviceopt",
             * "M": "DD:DD:DD:DD:DD:DD",
             * "U": "b562e3d8a4314e9e9ae6c3648855b2e1"
             }
             */
            String uuidMac="";
            if(uuid.length()<12)
            {
                uuidMac= UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
            }else{
                uuidMac=uuid.toUpperCase();
            }
            String mac=uuidMac.substring(0,2)+":"+uuidMac.substring(2,4)+":"+uuidMac.substring(4,6)+":"+uuidMac.substring(6,8)+":"+uuidMac.substring(8,10)+":"+uuidMac.substring(10,12);
            urlParams.put("C", "Device");
            Map<String, Object> D = new LinkedHashMap<>();
            D.put("AudioOut", 2.1);
            urlParams.put("D", D);
            urlParams.put("F", "SetDeviceopt");
            urlParams.put("M", mac);
            urlParams.put("U", uuid);
            String resultString = ystMovieService.getSettingByShipServer(shipServerUrl, urlParams);
            return AdminV1Result.success(resultString);
        }
        if(apiNum==3){
            if(!map.containsKey("mac"))
            {
                result.setCode(1);
                result.setMsg("mac请求参数不能为空");
                return result;
            }
            if(!map.containsKey("filmsource"))
            {
                result.setCode(1);
                result.setMsg("filmsource请求参数不能为空");
                return result;
            }
            String mac=CommonUtils.parseStringData(map.get("mac"));
            String filmsource=CommonUtils.parseStringData(map.get("filmsource"));
            Map<String, Object> urlParams = new LinkedHashMap<>();
            urlParams.put("mac", mac);
            urlParams.put("filmsource", filmsource);
            String resultString = ystMovieService.getGroupTypeListByShipServer(shipServerUrl, urlParams);
            return AdminV1Result.success(resultString);
        }
        if(apiNum==4){
            if(!map.containsKey("mac"))
            {
                result.setCode(1);
                result.setMsg("mac请求参数不能为空");
                return result;
            }
            if(!map.containsKey("filmsource"))
            {
                result.setCode(1);
                result.setMsg("filmsource请求参数不能为空");
                return result;
            }
            String mac=CommonUtils.parseStringData(map.get("mac"));
            String filmsource=CommonUtils.parseStringData(map.get("filmsource"));
            Map<String, Object> urlParams = new LinkedHashMap<>();
            urlParams.put("mac", mac);
            urlParams.put("filmsource", filmsource);
            String resultString = ystMovieService.getFilmListByTypeByShipServer(shipServerUrl, urlParams);
            return AdminV1Result.success(resultString);
        }
        if(apiNum==5){
            if(!map.containsKey("mac"))
            {
                result.setCode(1);
                result.setMsg("mac请求参数不能为空");
                return result;
            }
            if(!map.containsKey("filmsource"))
            {
                result.setCode(1);
                result.setMsg("filmsource请求参数不能为空");
                return result;
            }
            if(!map.containsKey("movieId"))
            {
                result.setCode(1);
                result.setMsg("movieId请求参数不能为空");
                return result;
            }
            String mac=CommonUtils.parseStringData(map.get("mac"));
            String filmsource=CommonUtils.parseStringData(map.get("filmsource"));
            String movieId=CommonUtils.parseStringData(map.get("movieId"));
            Map<String, Object> urlParams = new LinkedHashMap<>();
            urlParams.put("mac", mac);
            urlParams.put("filmsource", filmsource);
            urlParams.put("movieId", movieId);
            String resultString = ystMovieService.getFilmDetailByShipServer(shipServerUrl, urlParams);
            return AdminV1Result.success(resultString);
        }

        if(apiNum==6){
            if(!map.containsKey("mac"))
            {
                result.setCode(1);
                result.setMsg("mac请求参数不能为空");
                return result;
            }
            if(!map.containsKey("filmsource"))
            {
                result.setCode(1);
                result.setMsg("filmsource请求参数不能为空");
                return result;
            }
            if(!map.containsKey("movieId"))
            {
                result.setCode(1);
                result.setMsg("movieId请求参数不能为空");
                return result;
            }
            String mac=CommonUtils.parseStringData(map.get("mac"));
            String filmsource=CommonUtils.parseStringData(map.get("filmsource"));
            String movieId=CommonUtils.parseStringData(map.get("movieId"));
            Map<String, Object> urlParams = new LinkedHashMap<>();
            urlParams.put("mac", mac);
            urlParams.put("filmsource", filmsource);
            urlParams.put("movieId", movieId);
            String resultString = ystMovieService.getPlayUrlByShipServer(shipServerUrl, urlParams);
            return AdminV1Result.success(resultString);
        }
        if(apiNum==7){
            if(!map.containsKey("mac"))
            {
                result.setCode(1);
                result.setMsg("mac请求参数不能为空");
                return result;
            }
            if(!map.containsKey("filmsource"))
            {
                result.setCode(1);
                result.setMsg("filmsource请求参数不能为空");
                return result;
            }
            String mac=CommonUtils.parseStringData(map.get("mac"));
            String filmsource=CommonUtils.parseStringData(map.get("filmsource"));
            String keyword=CommonUtils.parseStringData(map.get("keyword"));
            Map<String, Object> urlParams = new LinkedHashMap<>();
            urlParams.put("mac", mac);
            urlParams.put("filmsource", filmsource);
            urlParams.put("show_model", 1);
            urlParams.put("keyword", keyword);

            String resultString = ystMovieService.searchFilmByShipServer(shipServerUrl, urlParams);
            return AdminV1Result.success(resultString);
        }
        if(apiNum==8){
            if(!map.containsKey("mac"))
            {
                result.setCode(1);
                result.setMsg("mac请求参数不能为空");
                return result;
            }
            if(!map.containsKey("filmsource"))
            {
                result.setCode(1);
                result.setMsg("filmsource请求参数不能为空");
                return result;
            }
            String mac=CommonUtils.parseStringData(map.get("mac"));
            String filmsource=CommonUtils.parseStringData(map.get("filmsource"));
            String size=CommonUtils.parseStringData(map.get("size"));
            Map<String, Object> urlParams = new LinkedHashMap<>();
            urlParams.put("mac", mac);
            urlParams.put("filmsource", filmsource);
            urlParams.put("size", size);

            String resultString = ystMovieService.getRecommendFilmByShipServer(shipServerUrl, urlParams);
            return AdminV1Result.success(resultString);
        }

        result.setCode(1);
        result.setMsg("参数错误！");
        return result;
    }

}
